#!/bin/sh
set -e
if [ -z "$SYSROOT" ]; then export SYSROOT="$(realpath "$(dirname "$0")/../sysroot")"; fi
if [ -z "$LLVMDIR" ]; then export LLVMDIR="$(realpath "$(dirname "$0")/../llvm-project")"; fi
if [ -f "$SYSROOT/bin/musl-gcc" ]; then export CC="$SYSROOT/bin/musl-gcc"; fi
if [ -f "$SYSROOT/bin/musl-clang" ]; then export CC="$SYSROOT/bin/musl-clang"; fi

rm -rf build2
mkdir build2
cd build2
export CFLAGS="-w -mcmodel=large -mstack-protector-guard=global -fno-pic -fno-pie -fcf-protection=none"
export CXXFLAGS="-w -mcmodel=large -mstack-protector-guard=global -fno-pic -fno-pie -fcf-protection=none -fno-use-cxa-atexit -I$SYSROOT/include/c++/v1/"
export LDFLAGS="-no-pie"

cmake \
	-DCMAKE_C_COMPILER="$CC" \
	-DCMAKE_CXX_COMPILER="$CC" \
	-DLLVM_ENABLE_RUNTIMES="libunwind" \
	-DLIBCXXABI_ENABLE_SHARED=OFF \
	-DLIBCXXABI_BAREMETAL=ON \
	-DLIBCXXABI_SILENT_TERMINATE=ON \
	-DLIBCXXABI_USE_LLVM_UNWINDER=ON \
	-DLIBCXXABI_USE_COMPILER_RT=ON \
	-DLIBCXXABI_INCLUDE_TESTS=OFF \
	-DCMAKE_INSTALL_PREFIX="$SYSROOT" \
	$LLVMDIR/libcxxabi
